home *** CD-ROM | disk | FTP | other *** search
- Path: noc.netcom.net!news
- From: Tarang Deshpande <tarang@willows.com>
- Newsgroups: comp.lang.c
- Subject: Re: Pascal -> C conversion
- Date: Wed, 17 Apr 1996 18:15:34 -0700
- Organization: NETCOM Network Operations
- Message-ID: <317597B6.38BC@willows.com>
- References: <4l3pb3$skd@news.bu.edu>
- NNTP-Posting-Host: daffy.willows.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB2 (Win95; I)
-
- J. Ram wrote:
- >
- > Hello netters:
- >
- > I have a simple 23 line pascal program I wrote 5 years ago that
- > I'd like to CONVERT to C. Can someone help since I'm not well
- > versed with the C syntax. All that needs to be done is change of syntax
- > without any change in programming style or approach to the problem.
- >
- > This pascal program uses a simple circular linked lists to solve the "Josephus
- > Problem".
- >
- > Start of program
- >
- > ----------------------------------------------------------------------------
- > August 25, 1991
- >
- > The program is a short program that solves the "Josephus Problem"
- > For the Josephus problem, we can imagine that N people have decided to commit
- > mass suicide by arranging themselves in circle and killing the M th person
- > around the circle, closing ranks as each person drops out of the circle.
- > The problem is to find out which person is the last to die or more importantly
- > to find the order in which the people were executed.
- >
- > Example
- > for N=9 and M=5, the order of execution is 5,1,7,4,3,6,9,2,8
- > /* Note that the print out will not produce ',' between numbers since it
- does exactly what the code above does. If you actually want the
- ',' change the statement:
- printf ( "%d", (t->next)->key );
- to:
- printf ( "%d,", (t->next)->key );
- */
-
- #include <stdio.h>
-
- typedef struct _NODE
- {
- int key;
- struct _NODE *next;
- } NODE;
-
- int main ( void )
- {
- int i, N, M;
- NODE *t, *x;
-
- scanf ( "%d %d", &N, &M );
- t = ( NODE* )malloc ( sizeof ( NODE ) );
- t->key = 1;
- x = t;
- for ( i = 2; i <= N; i++ )
- {
- t->next = ( NODE* )malloc ( sizeof ( NODE ) );
- t = t->next;
- t->key = i;
- }
- t->next = x;
- while ( t != t->next )
- {
- for ( i = 1; i < M; i++ )
- t = t->next;
- printf ( "%d", (t->next)->key );
- x = t->next;
- t->next = (t->next)->next;
- free ( x );
- }
- printf ( "%d\n", t->key );
- }
-